package vn.appsmobi.ulti;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
 
public class Request {
	  protected static final int HTTP_REQUEST_DELAY_MS = 5000;
	  protected static final int HTTP_REQUEST_TIMEOUT_MS = 30000;
	  public static final int STATUS_CLIENT_ERROR = 2;
	  public static final int STATUS_NETWORK_UNAVAILABLE = 4;
	  public static final int STATUS_NOT_MODIFIED = 7;
	  public static final int STATUS_OK = 0;
	  public static final int STATUS_PARAM_ERROR = 3;
	  public static final int STATUS_SERVER_ERROR = 1;
	  public static final int STATUS_SERVICE_UNAVAILABLE = 5;
	  public static final int STATUS_UNKNOWN_ERROR = 6;
	
	  private 	HttpURLConnection con;
	  private 	String response;
	  private 	int statusCode;
	  
	  private void readStream(InputStream in) {
		  BufferedReader reader = null;
		  try {
		    reader = new BufferedReader(new InputStreamReader(in));
		    String line = "";
		    while ((line = reader.readLine()) != null) {
		      System.out.println(line);
		    }
		  } catch (IOException e) {
		    e.printStackTrace();
		  } finally {
		    if (reader != null) {
		      try {
		        reader.close();
		      } catch (IOException e) {
		        e.printStackTrace();
		        }
		    }
		  } 
	  } 
    private  String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader;
		try {
				reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
				StringBuilder sb = new StringBuilder();
				 
		        String line = null;
		        try {
		            while ((line = reader.readLine()) != null) {
		                sb.append(line + "\n");
		            }
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                is.close();
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
		        return sb.toString();
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		return null;               
    }
 
    public  int getStatus()
    {
    	try{
    		
        	switch (this.statusCode) {
    			case HttpStatus.SC_OK:			
    				return STATUS_OK;
    			case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:			
    				return STATUS_SERVER_ERROR;
    			case HttpStatus.SC_SERVICE_UNAVAILABLE:			
    				return STATUS_SERVICE_UNAVAILABLE;
    			default:			
    				return STATUS_UNKNOWN_ERROR;	
    		}    		
    	}catch(Exception e){
    		return STATUS_NETWORK_UNAVAILABLE;
    	}
    	
    }
    
    
    
    /* This is a test function which will connects to a given
     * rest service and prints it's response to Android Log with
     * labels "Praeda".
     */
    public  Request(String strUrl)
    { 
    	try {
    		  URL url = new URL(strUrl);
    		  this.con = (HttpURLConnection) url
    		    .openConnection();
    		  	this.con.connect();
    		  	this.response = convertStreamToString(this.con.getInputStream());
		  		this.statusCode = this.con.getResponseCode();    		  	
    		  } catch (Exception e) {
    		  e.printStackTrace();
    		}
	
    }
    
    public String getResponse()
    {
    	if(this.statusCode == HttpStatus.SC_OK)
    		return this.response;
    	return "";
    }
 
}

